﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EasyErp.ViewModel;
using EasyErp.Domain;

namespace EasyErp.Controllers
{
    public class GumSheetController : Controller
    {
        erpEntities db = new erpEntities();
        GumSheetRepository gumSheetRepository = new GumSheetRepository();

        //
        // GET: /GumSheet/

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(GumSheetViewModel gumSheetViewModel)
        {
            int _employeeId = gumSheetRepository.GetEmployeeId(gumSheetViewModel.EmployeeNo);
            int _operationId = gumSheetRepository.GetOperationId(gumSheetViewModel.OperationNo);

            GumSheet GumSheet = new GumSheet 
            {
                EmployeeId = _employeeId,
                OperationId = _operationId,
                Section = gumSheetViewModel.Section,
                Date = gumSheetViewModel.Date
            };

            db.GumSheet.Add(GumSheet);
            db.SaveChanges();

            GumSheetProduction gumSheetProduction = new GumSheetProduction(); 

            foreach (var item in gumSheetViewModel.GumSheetProductionList)
            {
                if (item.BundleId != 0)
                {
                    gumSheetProduction = item;
                    gumSheetProduction.GumSheetId = GumSheet.GumSheetId;

                    // Add Data into GumSheetProduction
                    db.GumSheetProduction.Add(gumSheetProduction);
                    db.SaveChanges();
                }
            }

            GumSheetOffStandard gumSheetOffStandard = new GumSheetOffStandard();

            foreach (var item in gumSheetViewModel.GumSheetOffStandardList)
            {
                if (item.NonStandCode != null)
                {
                    gumSheetOffStandard = item;
                    gumSheetOffStandard.GumSheetId = GumSheet.GumSheetId;

                    // Add Data into GumSheetOffStandard
                    db.GumSheetProduction.Add(gumSheetProduction);
                    db.SaveChanges();
                }
            }

            return View();
        }

    }
}
